package se.rende.myrss.data;

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

public class MyRSSDataService extends IntentService {

	private int result = Activity.RESULT_CANCELED;
	private MyRSSDataHelper data;

	public MyRSSDataService() {
		super("MyRSSDataService");
		data = new MyRSSDataHelper(this);
	}

	// Will be called asynchronously be Android
	@Override
	protected void onHandleIntent(Intent intent) {
		String channelId = intent.getStringExtra("channelId");
		Log.d("MyRSSDataService", "channelId=" + channelId);
		try {
			ChannelToDbLoader channelToDbLoader = new ChannelToDbLoader(data);
			
			channelToDbLoader.load("http://10.0.2.2:8888/myrss_server", channelId);
//			channelToDbLoader.load("http://myrss-server.appspot.com/myrss_server", channelId);
			
			result = Activity.RESULT_OK;
		} catch (Exception e) {
			e.printStackTrace();
			result = Activity.RESULT_CANCELED;
		}

		Bundle extras = intent.getExtras();
		if (extras != null) {
			Messenger messenger = (Messenger) extras.get("MESSENGER");
			Message msg = Message.obtain();
			msg.arg1 = result;
			msg.obj = "not used yet";
			try {
				messenger.send(msg);
			} catch (android.os.RemoteException e1) {
				Log.w(getClass().getName(), "Exception sending message", e1);
			}

		}
	}
}